const amapFile = require('../libs/amap-wx.js');

class AMapUtil {
  constructor() {
    this.amapInstance = new amapFile.AMapWX({
      key: 'b3a66238201d41f3124985707f59000d' // 需要替换为新的有效 key
    });

    // POI类型码映射表
    this.POI_TYPES = {
      '加油站': '010100',
      '汽车服务': '030000',
      '汽车维修': '030200',
      '洗车场': '030400',
      '停车场': '150900'
    };
  }

  // 地址解析
  geocoder(address) {
    console.log('调用地址解析API:', {
      method: 'getInputtips',
      params: { keywords: address }
    });

    return new Promise((resolve, reject) => {
      this.amapInstance.getInputtips({
        keywords: address,
        success: res => {
          console.log('地址解析返回数据:', res);
          resolve(res);
        },
        fail: err => {
          console.error('地址解析失败:', err);
          reject(err);
        }
      });
    });
  }

  // 逆地址解析
  reverseGeocoder(location) {
    console.log('调用逆地址解析API:', {
      method: 'getRegeo',
      params: { location: `${location.longitude},${location.latitude}` }
    });

    return new Promise((resolve, reject) => {
      this.amapInstance.getRegeo({
        location: `${location.longitude},${location.latitude}`,
        success: res => {
          console.log('逆地址解析返回数据:', res);
          resolve({
            result: {
              address: res[0].desc,
              formatted_addresses: {
                recommend: res[0].name
              },
              address_component: {
                city: res[0].regeocodeData.addressComponent.city
              }
            }
          });
        },
        fail: err => {
          console.error('逆地址解析失败:', err);
          reject(err);
        }
      });
    });
  }

  // 搜索周边POI
  searchNearby(keyword, location, radius = 5000) {
    return new Promise((resolve, reject) => {
      const searchParams = {
        keywords: keyword,
        location: `${location.longitude},${location.latitude}`,
        querytypes: '010100', // 加油站POI类型码
        radius: radius,
        success: res => {
          console.log('周边搜索返回数据:', res);
          
          if (res && res.markers && Array.isArray(res.markers)) {
            const pois = res.markers
              .filter(marker => 
                marker.name.toLowerCase().includes(keyword.toLowerCase()) ||
                marker.address.toLowerCase().includes(keyword.toLowerCase())
              )
              .map((marker, index) => {
                const poiData = res.poisData[index] || {};
                return {
                  id: marker.id || poiData.id,
                  name: marker.name || poiData.title,
                  address: marker.address || poiData.address,
                  distance: marker.distance || poiData.distance,
                  latitude: marker.latitude || poiData.location?.lat,
                  longitude: marker.longitude || poiData.location?.lng,
                  tel: poiData.tel || '',
                  type: poiData.type || '',
                  image: (poiData.photos && poiData.photos[0]?.url) || '/static/index_活动2.png',
                  business_hours: poiData.business_hours || '全天营业',
                  price: '5.66',
                  discounts: ['满200减20', '满500减60']
                };
              });

            resolve({
              detail: { pois }
            });
          } else {
            reject(new Error('未找到加油站数据'));
          }
        },
        fail: err => {
          console.error('周边搜索失败:', err);
          reject(err);
        }
      };

      console.log('搜索参数:', searchParams);
      this.amapInstance.getPoiAround(searchParams);
    });
  }
}

export default new AMapUtil(); 